package info.casel.login;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SignIn
 */
@WebServlet("/SignIn")
public class SignIn extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SignIn() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session = request.getSession();
		checkSession(request, response, session);
		
		
		
	}

	private void checkSession(HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		if (null != session.getAttribute("login")){
			System.out.println("Ok Guys Lets Do It");
		this.getServletContext().getRequestDispatcher("/private.jsp").forward(request, response);
		} else { 
			//login incorrect
			request.setAttribute("error", "Login incorrect");
			this.getServletContext().getRequestDispatcher("SingIn").forward(request,  response);
		}
		this.getServletContext().getRequestDispatcher("/private.jsp").forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		HttpSession session = request.getSession();
		PrintWriter output = response.getWriter();
		

		String login = request.getParameter("user");
		String password = request.getParameter("password");
		
		
		Pattern pattern = Pattern.compile("[a-z]{8}");
		Matcher loginChecker = pattern.matcher(login);
		Matcher passwordChecker = pattern.matcher(password);
		boolean anyErrors = false;
		
		
	
		//chet login value
		if (!loginChecker.find()){
			request.setAttribute("loginerror", "Login");
			this.getServletContext().getRequestDispatcher("/Login.jsp").forward(request, response);	
			anyErrors= true;
		}
		//check password value
		
		if (!passwordChecker.find()){
			request.setAttribute("passwordvalue", "Password");
			this.getServletContext().getRequestDispatcher("/Login.jsp").forward(request, response);	
			anyErrors=true;
		}
		
		if (anyErrors){
			this.getServletContext().getRequestDispatcher("/login.jsp").forward(request,response);
			return;
		}
		
		
		
		
		
		if (login.equals("Dunkan") && password.equals("McLeod")) {
			session.setAttribute("login", login);			
			this.getServletContext().getRequestDispatcher("/private.jsp").forward(request, response);
			System.out.println("login correct");
		} else {
			this.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);	
			System.out.println("ERROR");
		}
	
	
	
	
	}

}
